import React, { Component } from "react";

class App extends Component {
  constructor() {
    super();
    console.log("1.加载组件默认状态");
    this.state = {
      num: 0,
    };
  }

  UNSAFE_componentWillMount() {
    console.log("2.组件将要挂载");
  }
  componentDidMount() {
    console.log("3.组件挂载完成");
  }
  shouldComponentUpdate(nextProps, nextState) {
    console.log("4.是否更新组件");
    console.log(nextState.num);
    return nextState.num % 2 === 0;
  }
  UNSAFE_componentWillUpdate() {
    console.log("5.组将要更新");
  }
  componentDidUpdate() {
    console.log("6.组件更新完成毕");
  }
  addnum = () => {
    this.setState({
      num: this.state.num + 1,
    });
  };
  render() {
    console.log("渲染");
    return (
      <>
        <h2>生命周期</h2>
        <h4>{this.state.num}</h4>
        <button onClick={this.addnum}>+1</button>
      </>
    );
  }
}
export default App;
